<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>用户登录</title>
     <link rel="stylesheet" type="text/css" href="../css/amazeui.min.css" />
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
     <link rel="stylesheet" type="text/css" href="../css/tngou.css" />
</head>
<body>

<div   class="am-form am-margin" >
  
  
    <legend class="am-text-center   am-text-danger">用户登录 </legend>
    <div class="am-input-group am-form-group am-input-group-danger">
     <span class="am-input-group-label"><i class="am-icon-user am-icon-fw"></i></span>
      <input type="text" id="name" name="name"  placeholder="输入邮件/账号" class="am-form-field" required/>
    </div>
    
     <div class="am-input-group am-form-group am-input-group-danger">
     <span class="am-input-group-label"><i class="am-icon-lock am-icon-fw"></i></span>
      <input type="password" id="password" name="password"  placeholder="输入登录密码" class="am-form-field" required/>
    </div>
    <div class="am-form-group">
    <button class="am-btn am-btn-danger am-radius am-btn-block" type="button" id="tngou-login" onclick="login();">登 录</button>
    <p class="am-text-sm am-text-secondary">天狗云用户第三方登录认证。</p>
    </div>
    <hr>
 <div class="am-form-group">
  <button class="am-btn am-btn-warning am-radius am-btn-block" type="button" id="tngou-reg" onclick="openFrame('./reg.html','reg');">新用户注册</button>
   <p class="am-text-sm am-text-success">如果你不是指天狗云用户，请点击这里进行用户注册。</p>
  </div>
  
 
</div>


</body>
<script type="text/javascript" src="../script/api.js"></script>
<script src="../script/jquery.min.js"></script>
<script src="../script/amazeui.min.js"></script>
<script src="../script/config.js"></script>
<script type="text/javascript">
  var client_id,client_secret;
	apiready = function(){
		
		    client_id = API_client_id;
        	client_secret= API_client_secret;
 
	};

function login(){   	
         var name=$("#name").val();
	     if(name.length==0)
	     {
	     	$("#name").focus();
	       api.toast({msg : '用户名不能为空'});
           return;
	     }
	     var password=$("#password").val();
	     if(password.length==0)
	     {
	       $("#password").focus();
	       api.toast({msg : '密码不能为空'});
           return;
	     }
	     
	      api.showProgress({
		    style: 'default',
		    animationType: 'fade',
		    title: '登录中...',
		    text: '请稍候...',
		    modal: false
		});
		api.ajax({
			    url: OpenAPI.oauth_login,
			    method: 'post',
			    timeout: 30,
			    dataType: 'json',
			    returnAll:false,
			    data:{
			        values: {name: name,password: password,client_id:client_id,client_secret:client_secret}
			    }
			},function(ret,err){
			   
			    if (ret) { 
			        if(ret.status)
			        {
//			          api.alert({msg: JSON.stringify(ret)});
			           var access_token=ret.access_token;
			          $api.setStorage('access_token', ret.access_token);	// 设置cookie
			           api.ajax({
			              url: OpenAPI.user,
			              method: 'post',
			              dataType: 'json',
			              data:{
					        values: {access_token: access_token}
					    	}
					    },function(ret,err){
					     
					        api.hideProgress();
//                          api.alert({msg: JSON.stringify(ret)});
					    	if(ret.status)
					    	{
					    	   
						       api.setPrefs({
								    key: 'user',
								    value: ret
								});																
								api.closeWin();
							}		              
					              
					     });
			           
			           
			        }else
			        {
			          api.hideProgress();
			          api.toast({msg : ret.msg});
			        }
			       
			    }else {
			        api.hideProgress();
			        api.alert({
			            msg:('错误码：'+err.code+'；错误信息：'+err.msg+'网络状态码：'+err.statusCode)
			        });
			    }
			});
		
		
    }

	

	
	
</script>
</html>